﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleService
{
    public class Server
    {
        public int server_id { get; set; }
        public int group_id { get; set; }
        public string name {get; set;}
        public string path { get; set; }
        public int server_port { get; set; }
        public string executable { get; set; }
        public string config_path { get; set;}
        public string template_path { get; set; }
        public bool is_starting { get; set; }
        public bool is_stopping {get; set; }
        public bool is_updating { get; set; }
        public bool is_started { get; set; }
        public bool is_stopped { get; set; }
        public DateTime last_datetime { get; set; }

        public List<Template> templates { get; set; }

        public Server()
        {
            this.templates = new List<Template>();
        }

        public void Update(Server server)
        {
            this.group_id = server.group_id;
            this.name = server.name;
            this.server_port = server.server_port;
            this.path = server.path;
            this.executable = server.executable;
            this.config_path = server.config_path;
            this.template_path = server.template_path;

            if (server.templates != null && server.templates.Count > 0)
            {
                if (this.templates == null)
                {
                    this.templates = new List<Template>();
                }
                foreach (var template in server.templates)
                {
                    var position = -1;
                    for (var i = 0; i < this.templates.Count; i++)
                    {
                        if (this.templates[i].template_id == template.template_id)
                        {
                            position = i;
                            break;
                        }
                    }
                    if (position < 0)
                    {
                        this.templates.Add(template);
                    }
                    else
                    {
                        this.templates[position] = template;
                    }
                }
            }
        }

        public string[] getJSON()
        {
            return new string[]{
                "server_id",
                "group_id",
                "name",
                "path",
                "executable",
                "config_path",
                "template_path",
                "is_starting",
                "is_stopping",
                "is_updating",
                "is_started",
                "is_stopped",
                "last_datetime",
            };
        }
    }
}
